package com.jrx.anytxn.transaction.timer.job.queryRefundStatus.config;

import com.jrx.anytxn.param.entity.PrCodeTable;
import com.jrx.anytxn.param.service.system.ICodeTableService;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.bean.LoanSaveBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.entity.TlRefundLog;
import com.jrx.anytxn.transaction.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.timer.job.queryRefundStatus.step.QueryRefundStatusProcessor;
import com.jrx.anytxn.transaction.timer.job.queryRefundStatus.step.QueryRefundStatusWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 溢缴款查询任务
 *
 * @author liuzb
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class QueryRefundStatusConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    DataSource dataSource;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    public GrayVersionConfig grayVersionConfig;

    @Bean
    public Job queryRefundStatusJob() {
        return jobBuilderFactory.get("queryRefundStatusJob")
                .start(queryRefundStatusStep())
                .build();
    }

    @Bean
    public Step queryRefundStatusStep() {
        return stepBuilderFactory.get("queryRefundStatusStep")
                .<TlRefundLog,TlRefundLog>chunk(anytxnBatchProperties.getQueryRefundStatusConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(queryRefundStatusReader())
                .processor(queryRefundStatusProcessor())
                .writer(queryRefundStatusWriter())
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getQueryRefundStatusConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlRefundLog> queryRefundStatusReader() {
        //同步reader   线程安全
        MyBatisCursorItemReader<TlRefundLog> accrualCursorReader = new MyBatisCursorItemReader<>();
        accrualCursorReader.setSaveState(false);
        accrualCursorReader.setSqlSessionFactory(sqlSessionFactory);
        accrualCursorReader.setQueryId("com.jrx.anytxn.transaction.mapper.ext.ExtTlRefundLogMapper.selectBySumStatus");
        Map<String, Object> map = new HashMap<>();
        map.put("sumStatus", TransBizConstant.PAYMENT_LOG_SUM_STATUS_O);
        map.put("grayVersion", grayVersionConfig.getCurrentVersion());
        accrualCursorReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlRefundLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(accrualCursorReader);
        return synchronizedItemStreamReader;
    }

    @Bean
    @StepScope
    public QueryRefundStatusProcessor queryRefundStatusProcessor() {
        return new QueryRefundStatusProcessor();
    }


    @Bean
    @StepScope
    public ItemWriter<TlRefundLog> queryRefundStatusWriter() {
        return new QueryRefundStatusWriter();
    }

}
